package io.renren.modules.xk.controller;


import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.shiro.ShiroUtils;
import io.renren.modules.xk.entity.XkCourseEntity;
import io.renren.modules.xk.service.XkCourseService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 课程维护
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/xk/course")
public class XkCourseController {
	@Autowired
	private XkCourseService xkCourseService;

	/**
	 * 所有课程列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("xk:course:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = xkCourseService.queryPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 课程信息
	 */
	@RequestMapping("/info/{courseId}")
	@RequiresPermissions("xk:course:info")
	public R info(@PathVariable("courseId") Long courseId){
		XkCourseEntity course = xkCourseService.getById(courseId);
		return R.ok().put("course", course);
	}
	
	/**
	 * 保存课程
	 */
	@SysLog("保存课程")
	@RequestMapping("/save")
	@RequiresPermissions("xk:course:save")
	public R save(@RequestBody XkCourseEntity course){
		ValidatorUtils.validateEntity(course, AddGroup.class);

		xkCourseService.saveEntity(course);
		
		return R.ok();
	}
	
	/**
	 * 修改课程
	 */
	@SysLog("修改课程")
	@RequestMapping("/update")
	@RequiresPermissions("xk:course:update")
	public R update(@RequestBody XkCourseEntity course){
		ValidatorUtils.validateEntity(course, UpdateGroup.class);

		xkCourseService.update(course);
		
		return R.ok();
	}
	
	/**
	 * 删除课程
	 */
	@SysLog("删除课程")
	@RequestMapping("/delete")
	@RequiresPermissions("xk:course:delete")
	public R delete(@RequestBody Long[] courseIds){

		xkCourseService.removeByIds(Arrays.asList(courseIds));
		
		return R.ok();
	}
}
